package com.cvsher.springtest.dao.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate4.HibernateTemplate;
import org.springframework.stereotype.Repository;

import com.cvsher.springtest.bean.User;
import com.cvsher.springtest.dao.IUserDao;

@Repository("userDao")
public class UserDao implements IUserDao{
	
	@Autowired
	protected HibernateTemplate hibernateTemplate;

	@Override
	public void save(User user) {
		hibernateTemplate.save(user);
	}

	@Override
	public void update(User user) {
		hibernateTemplate.update(user);
	}

	@Override
	public void saveOrUpdate(User user) {
		hibernateTemplate.saveOrUpdate(user);
	}

	@Override
	public void deleteById(long id) {
		User user = hibernateTemplate.get(User.class, id);
		hibernateTemplate.delete(user);
	}

	@Override
	public List<User> getAllUser() {
		return hibernateTemplate.loadAll(User.class);
	}

	@Override
	public List<User> getUsersByName(String name) {
		final String hql = "from User u where u.name='"+name+"'";
		return (List<User>)hibernateTemplate.find(hql);
	}
	
	
}
